/**
 * 
 * 服务器访问地址
 */
var forumHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";

var j = 1;
/**
 * base64 加密对象初始化
 */
var b64 = new Base64();
var doTime = new Date();



/**
 * 网络请求函数
 * @param {Object} url
 * @param {Object} callback
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 更新论坛版块列表
 */
function  getForumList(){
	var samecity = getLocalStorge('samecity');
	var ftime = getLocalStorge('ftime');
	var nowTime = doTime.getTime();
	var disTime = nowTime - parseInt(ftime);
	if((disTime < 86400000) && samecity){
		showForum();
	}else{
		var url = forumHost + '&ac=forumlist';
		xmlHttp(url,showList);
	}	
}

/**
 * 板块网络请求的回调函数
 * @param {Object} data
 */
function showList(data){
		localStorage.setItem('samecity',data.samecity);
		localStorage.setItem('second',data.second);
		localStorage.setItem('rent',data.rent);
		localStorage.setItem('recruit',data.recruit);
		localStorage.setItem('life',data.life);
		localStorage.setItem('deal',data.deal);
		localStorage.setItem('yule',data.yule);
		localStorage.setItem('share',data.share);
		localStorage.setItem('opinion',data.opinion);
		showForum();
		var nowTime = doTime.getTime();
		localStorage.setItem('ftime',nowTime);
}

/**
 * 显示各个板块下的板块列表
 */
function showForum(){
	showForumByType('samecity');
	showForumByType('second');
	showForumByType('rent');
	showForumByType('recruit');
	showForumByType('life');
	showForumByType('deal');
	showForumByType('yule');
	showForumByType('share');
	showForumByType('opinion');
	
	//var forumDom = $('#opinion ul');
	//var li = '<li ontouchstart="zy_touch(\'btn-newact\')" onclick="openForumList(3)"><img src="css/forum/3.jpg" /><p>回收站</p></li>';
	//forumDom.append(li);
}

/**
 * 根据类型显示对应的板块列表
 * @param {Object} type
 */
function showForumByType(ftype){
	    var forumList = getLocalStorge(ftype);
		if(!forumList){
			return;
		}
		
		var forumArr = new Array();
		var forumParam = new Array();
		forumArr = forumList.split('__');
		var forumDom = $('#'+ftype+' ul');
		forumDom.html('');
		var thumb;
		for(var i=0;i<forumArr.length;i++){
			forumParam = forumArr[i].split('||');
			if(typeof(forumParam[2]) != 'undefined'){
				thumb = 'http://www.yeeyi.com/bbs/static/image/forum/'+forumParam[2]+'.jpg';
			}else{
				thumb = 'css/forum/'+forumParam[0]+'.jpg';
			}
			var li = '<li ontouchstart="zy_touch(\'btn-newact\')" onclick="openForumList('+forumParam[0]+')"><img src="'+thumb+'" /><p>'+b64.decode(forumParam[1])+'</p></li>';
			forumDom.append(li);
		}
}

/**
 * 打开板块列表
 * @param {Object} fid
 */
function openForumList(fid){
		localStorage.setItem('fid',fid);
		uexWindow.open(fid,'0',fid+'.html','2','100%','100%','4');
}

/**
 * 获取头部滚动帖子
 */
function getHeadThread(){
	var url = forumHost + '&ac=fheader';
	xmlHttp(url,showHeadThread);
}

/**
 * 获取头部缓存数据
 */
function showLocalHead(headStr){
	var headThred = new Array();
	var headParam = new Array();
	headThread = headStr.split('__');
	for(var i=0;i<headThread.length;i++){
		headParam = headThread[i].split('||');
		$(".m_"+i.toString()).attr('id',headParam[0]);
		$(".m_"+i.toString()+" img").attr('src',b64.decode(headParam[1]));
		$(".m_"+i.toString()+" p").html(b64.decode(headParam[2]));
	}
}

/**
 * 头部滚动帖子请求的回调函数
 * @param {Object} items
 */
function showHeadThread(items){
	var headStr = '';
	for(var i in items){
		var item = items[i];
		$(".m_"+i.toString()).attr('id',item.tid);
		//$(".m_"+i.toString()+" img").attr('src',item.thumb);
		//头部新闻图片缓存
		imgCache('p'+i,item.thumb);
		$(".m_"+i.toString()+" p").html(b64.decode(item.subject));
	}
}

/**
 * 图片缓存
 * @param {Object} rid
 * @param {Object} imgurl
 */
function imgCache(rid,imgurl){
	var extArr = new Array();
	extArr = imgurl.split('.');
	var ext = extArr[extArr.length-1];
	zy_imgcache(rid,imgurl,imgurl,changeImgDom,err,null,ext);
}

/**
 * 缓存成功的回调函数
 * @param {Object} rid
 * @param {Object} imgurl
 */
function changeImgDom(rid,imgurl){
	$('#'+rid).attr('src',imgurl);
}

/**
 * 缓存出错的缓存函数
 * @param {Object} rid
 * @param {Object} imgurl
 */
function err(rid,imgurl){
	$('#'+rid).attr('src',imgurl);
}

/**
 * 头部滚动贴的滚动控制函数
 */
function cycle(){
	if(j > 3){
		j = 1;
	}
	$('.m-carousel').carousel('move', j);
	j++;
}

/**
 * 打开帖子详细页
 * @param {Object} tid
 */
function goThreadWindow(tid){
	if(tid == ''){
		uexWindow.alert("ERROR","未指定帖子ID,无法加载帖子内容！","返回");
	}else{
		localStorage.setItem("tid",tid);
		var threadUrl = "viewthread.html";
		uexWindow.open("threadWindow",0,threadUrl,2,"","",4);
	}
}

/**
 * 封装的获取本地localStorage值
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}